$(document).ready(function(){
    if($.cookie('Authorization')===null || localStorage.getItem('realname') === null) {
        showErr("您处于未登录状态，请先登录", '1000000');
        return
    }
    $('#realname').text(localStorage.getItem('realname') )

    $('.entry-link-left').click(function(){
        localStorage.setItem('searchParam', JSON.stringify({
            pageNo: 1,
            pageSize:10
        }))
        window.location.href = host_url + "theme.html"
    })

    $('.entry-link-right').click(function(){
        window.location.href = host_url + "partner.html"
    })

    $('.entry-search-btn').click(function(){
        if ($('.entry-search-input').val() === '') {
            localStorage.setItem('searchParam', JSON.stringify({
                pageNo: 1,
                pageSize:10
            }))
        } else {
            localStorage.setItem('searchParam', JSON.stringify({
                pageNo: 1,
                pageSize:10,
                entName: $('.entry-search-input').val()
            }))
        }
        window.location.href = host_url + "search.html"
    })

    // 输入框响应回车
    $('.entry-search-input').keyup(function(event){
        if(event.keyCode ==13){
            $('.entry-search-btn').click()
        }
    })
    loadInteraction()
})

// 加载最新互动
function loadInteraction() {
    $.ajax({
        url: service_url + '/a/zhiku/zkLinkInteraction/list?ifindex=1&pageNo=1&pageSize=50&token=' + $.cookie('Authorization'),
        // url: service_url + '/a/zhiku/zkLinkInteraction/list?pageNo=1&pageSize=50&token=' + $.cookie('Authorization'),
        type: 'GET',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        contentType: 'application/json',
        dataType: 'jsonp',
        headers: {
            "Authorization": $.cookie('Authorization')
        },
        jsonpCallback:"success_jsonpCallback",  
        processData: false, // 告诉jQuery不要去处理发送的数据
        success: function(data) {
            console.log(data)
            if (data.status === "1000000" || data.status ==="01") {
                showErr("您处于未登录状态，请先登录", '1000000')
                return
            }
            
            var tempData = JSON.parse(data.data)
            if (tempData.count == 0) return
            var tempList = tempData.list
            var resultArr = []
            for (var i = 0; i < tempList.length; i++) {
                if (tempList[i].zkPictureMessageList !== undefined &&tempList[i].description !== undefined) {
                    if (tempList[i].zkPictureMessageList.length > 0) {
                        resultArr.push({
                            entId: tempList[i].entId,
                            id: tempList[i].zkPictureMessageList[0].id,
                            description: tempList[i].description.replace(/</g, '&lt;').replace(/>/g, '&gt;')
                        })
                    }

                }
            }
            // console.log('resultArr', resultArr)
            var tempHtml = '<ol class="carousel-indicators"><li data-target="#entryCarousel" data-slide-to="0" class="active"></li><li data-target="#entryCarousel" data-slide-to="1"></li><li data-target="#entryCarousel" data-slide-to="2"></li></ol><div class="carousel-inner">'
            for (var m = 0; m < resultArr.length; m = m+3) {
                if (m < 9) {
                    
                    if (m < 3) {
                        tempHtml = tempHtml + '<div class="item active"><div class="entry-item"><a href="detail.html?id='+resultArr[m].entId+'"><img src="'+service_url + '/a/zhiku/zkLinkInteraction/getPic?id=' + resultArr[m].id+'" ><p>'+resultArr[m].description+'</p></a><a href="detail.html?id='+resultArr[m].entId+'"><span>查看更多消息</span></a></div>'
                        if (m+1 < resultArr.length) {
                            tempHtml = tempHtml + '<div class="entry-item"><a href="detail.html?id='+resultArr[m+1].entId+'"><img src="'+service_url + '/a/zhiku/zkLinkInteraction/getPic?id=' + resultArr[m+1].id+'" ><p>'+resultArr[m+1].description+'</p></a><a href="detail.html?id='+resultArr[m+1].entId+'"><span>查看更多消息</span></a></div>'
                        }
                        if (m+2 < resultArr.length) {
                            tempHtml = tempHtml + '<div class="entry-item  entry-item-last"><a href="detail.html?id='+resultArr[m+2].entId+'"><img src="'+service_url + '/a/zhiku/zkLinkInteraction/getPic?id=' + resultArr[m+2].id+'" ><p>'+resultArr[m+2].description+'</p></a><a href="detail.html?id='+resultArr[m+2].entId+'"><span>查看更多消息</span></a></div>'
                        }
                        tempHtml = tempHtml + '</div>'
                    } else {
                        tempHtml = tempHtml + '<div class="item"><div class="entry-item"><a href="detail.html?id='+resultArr[m].entId+'"><img src="'+service_url + '/a/zhiku/zkLinkInteraction/getPic?id=' + resultArr[m].id+'" ><p>'+resultArr[m].description+'</p></a><a href="detail.html?id='+resultArr[m].entId+'"><span>查看更多消息</span></a></div>'
                        if (m+1 < resultArr.length) {
                            tempHtml = tempHtml + '<div class="entry-item"><a href="detail.html?id='+resultArr[m+1].entId+'"><img src="'+service_url + '/a/zhiku/zkLinkInteraction/getPic?id=' + resultArr[m+1].id+'" ><p>'+resultArr[m+1].description+'</p></a><a href="detail.html?id='+resultArr[m+1].entId+'"><span>查看更多消息</span></a></div>'
                        }
                        if (m+2 < resultArr.length) {
                            tempHtml = tempHtml + '<div class="entry-item  entry-item-last"><a href="detail.html?id='+resultArr[m+2].entId+'"><img src="'+service_url + '/a/zhiku/zkLinkInteraction/getPic?id=' + resultArr[m+2].id+'" ><p>'+resultArr[m+2].description+'</p></a><a href="detail.html?id='+resultArr[m+2].entId+'"><span>查看更多消息</span></a></div>'
                        }
                        tempHtml = tempHtml + '</div>'
                    }
                }
            }
            tempHtml = tempHtml + '</div>'
            $('#entryCarousel').append(tempHtml)
            $("#entryCarousel").carousel('cycle')
            $('#entryCarousel .loading_div').hide()
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest, textStatus, errorThrown)
            showErr("您处于未登录状态，请先登录", '1000000')
        }
    })
}